package com.tongji.diy.model.furniturePlacementOptimization.assembleComplement
{
	import com.tongji.diy.model.furniturePlacementOptimization.DataAndFunction;
	import com.tongji.diy.model.furniturePlacementOptimization.OptimizationParameter;
	import com.tongji.diy.utils.OptimizationMath;
	import com.tongji.diy.utils.PolygonMath;
	import com.tongji.diy.utils.RoomParameters;
	
	import flash.geom.Point;
	import flash.geom.Vector3D;

	public class NoAccessibleFurniture extends FurnitureBase
	{
		private var _onCeiling:Boolean = false;
		private var _radiation_radius:Number;
		private var _recoder:Vector3D = new Vector3D;
		public function NoAccessibleFurniture(furnitrueIndex:int, furnitureClass:int, aganistWall:Boolean, radiation_radius:Number,length:Number=0, width:Number=0, height:Number=0)
		{
			super(furnitrueIndex, furnitureClass, aganistWall,length, width, height);
			this._radiation_radius = radiation_radius;
			isOnCeiling();
			setHeight();
		}
		//根据家具类型判断时候是放在天花板上的
		private function isOnCeiling():void
		{
			switch(this.furnitureClass)
			{
				case OptimizationParameter.CEILING_LAMP:
					this._onCeiling = true;
					break;
				case OptimizationParameter.AIRCONDITION:
					this._onCeiling = true;
					break;
				default:
					this._onCeiling = false;
			}
		}
		//如果在天花板上则将物体z设置到天花板上
		private function setHeight():void
		{
			if(_onCeiling)
				this.z = RoomParameters.wallHeight - this.height;
		}
		
		private function writeRecoder():void
		{
			_recoder.x = this.x;
			_recoder.y = this.y;
			_recoder.z = this.angle;
		}
		//摆放家具位置，返回值是x，y，以及 anlge
		//为了简化处理将随机过程控制在100次
		public function arrange(SLList:Vector.<SLbase>, data:DataAndFunction):void
		{
			var maxNum:int = 200;
			var count:Number = -Infinity;
			if(anganistWall)
			{
				var v:Vector3D = data.searchLocationWall(this,Math.random());
				this.x = v.x; this.y = v.y; this.angle = v.z;
				writeRecoder();
			}
			else
				writeRecoder();
			for(var i:int = 0; i < maxNum; ++i)
			{
				//随机分配xy，只有在天花板上且不靠墙的家具才要这样摆放，否则一律靠边摆放
				if(_onCeiling && !anganistWall)
				{
					
					this.x = _recoder.x + OptimizationMath.normalRandom()*100;	
					this.y = _recoder.y + OptimizationMath.normalRandom()*100;
					this.angle = data._orientedSelectionList[Math.floor(Math.random()*data._orientedSelectionList.length)];
					while(!data.boundingBoxInRoom(this.accessibleBox, data._room))
					{
						this.x = _recoder.x + OptimizationMath.normalRandom()*100;	
						this.y = _recoder.y + OptimizationMath.normalRandom()*100;
						this.angle = data._orientedSelectionList[Math.floor(Math.random()*data._orientedSelectionList.length)];
					}
				}//if(_onCeiling)
				else
				{
					v = data.searchLocationWall(this,Math.random());
					this.x = v.x; this.y = v.y; this.angle = v.z;
				}
				
				var tempCount:int = 0;
				for(var j:int = 0; j < SLList.length; ++j)
				{
					if(PolygonMath.pointInCircular(new Point(SLList[j].x, SLList[j].y),this._radiation_radius,new Point(this.x, this.y)))
					{
						tempCount += SLList[j].peopleNum;
					}
				}//for
				
				if(tempCount > count)
				{
					count = tempCount;
					writeRecoder();
				}//if
				
			}//for(var i:int = 0; i < maxNum; ++i)
			trace("count",count);
			this.x = _recoder.x;
			this.y = _recoder.y;
			this.angle = _recoder.z;
		}
	}
}